package com.oscarmovie.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oscarmovie.model.Image;
import com.oscarmovie.model.Paging;

public class ImageDaoImpl extends HibernateDaoSupport implements ImageDao {

	public void save(Image image) {
		getSession().saveOrUpdate(image);
	}

	public Image load(int id) {
		Image image = (Image)getSession().load(Image.class, id);
		return image;
	}
	

	public List<Image> loadAll(Paging paging) {
		paging.setTotalCount((int) count());

		Query query = getSession().createQuery("from Image ");
		query.setCacheable(true);
		query.setFirstResult(paging.getCurrentRow()-1);
		query.setMaxResults(paging.getPageSize());
		List<Image> list = (List<Image>)query.list();
		return list;
	}

	private long count() {
		Query query = getSession().createQuery("select count(*) from Image");
		query.setCacheable(true);
		Long count = (Long) query.uniqueResult();
		return count;
	}
	
}
